<template>
  <div class="tab" v-loading="imgLoading">
    <div class="title" v-if="bannerInfoList?.off == 1">
      <span>选择课程类型</span>
      <span class="infos">课程类型的区别</span>
    </div>
    <div class="title" v-if="bannerInfoList?.off == 2">
      <span>购买详情</span>
    </div>

    <div class="tab" v-if="bannerInfoList?.off == 1">
      <div style="display: flex">
        <div
          class="tab_left"
          @click="tabs(0)"
          :class="index == 0 ? 'active' : ''"
        >
          <div class="info" :style="{ color: index == 0 ? '#f84658' : '' }">
            课程单次费用
          </div>
          <div class="price" :style="{ color: index == 0 ? '#f84658' : '' }">
            <i
              class="iconfont icon-qianqian-"
              :style="{ color: index == 0 ? '#f84658' : '' }"
            ></i
            >{{ detailList.price ?? '0.00' }}
          </div>
          <div class="strike-through">
            ¥{{ detailList.courseDiscountPrice ?? '0.00' }}
          </div>
          <div class="setInfo">购买全套更划算</div>
          <div class="discounts">限时优惠</div>
        </div>
        <div
          class="tab_left tab_center"
          :class="index == 1 ? 'active' : ''"
          @click="tabs(1)"
        >
          <div class="middle">
            <div>
              <div
                class="info center"
                :style="{ color: index == 1 ? '#f84658' : '' }"
              >
                课程全套费用
              </div>
              <div
                class="price center"
                :style="{ color: index == 1 ? '#f84658' : '' }"
              >
                <i
                  class="iconfont center icon-qianqian-"
                  :style="{ color: index == 1 ? '#f84658' : '' }"
                ></i
                >{{ resultPrice?.totalPrice?.discountedPrice ?? '0.00' }}
              </div>
              <div class="strike-through">
                ¥{{ resultPrice?.totalPrice?.price ?? '0.00' }}
              </div>
            </div>
            <div class="fuhao">≈</div>
            <div>
              <div class="info" style="font-size: 18px; color: #666666">
                单次课程平均费用
              </div>
              <div class="price" style="font-size: 36px; color: #666666">
                <i class="iconfont icon-qianqian-" style="color: #666666"></i
                >{{ length() }}
              </div>
              <div class="strike-through">
                ¥ {{ resultPrice?.totalPrice?.price ?? '0.00' }}
              </div>
            </div>
          </div>
          <div class="setInfo center">单次课程平均仅需约{{ length() }}元</div>
          <div class="discounts">加送视频特权服务</div>
        </div>
        <div class="more" :class="index == 2 ? 'active' : ''" @click="tabs(2)">
          <span class="more_txt">查 看 更 多 优 惠 套 餐</span>
        </div>
      </div>
    </div>

    <div class="cope" v-if="bannerInfoList?.off == 2">
      <div>
        <div
          class="tab_left"
          @click="tabs(0)"
          style="
            margin-right: 0;
            background: linear-gradient(180deg, #fef6f7 0%, #fef3f4 100%);
          "
          :class="index == 0 ? 'active' : ''"
        >
          <div class="info" :style="{ color: index == 0 ? '#f84658' : '' }">
            课程单次费用
          </div>
          <div class="price" :style="{ color: index == 0 ? '#f84658' : '' }">
            <i
              class="iconfont icon-qianqian-"
              :style="{ color: index == 0 ? '#f84658' : '' }"
            ></i
            >{{ detailList.price ?? '0.00' }}
          </div>
          <div class="strike-through">
            ¥{{ detailList.courseDiscountPrice ?? '0.00' }}
          </div>
          <div class="discounts">限时优惠</div>
        </div>
        <div class="money">
          <span class="money_info">应付金额</span>
          <span class="money_price">
            <span style="font-size: 12px"
              ><i
                class="iconfont icon-qianqian-"
                :style="{ color: index == 0 ? '#f84658' : '' }"
              ></i></span
            >{{ detailList.price ?? '0.00'
            }}<span style="font-size: 12px">元</span></span
          >
        </div>
      </div>
      <div class="code">
        <div class="qrCode_left">
          <div class="qrCode" v-if="codeUrl">
            <img :src="codeUrl" alt="" class="qrImage" />
            <p style="font-size: 12px">{{ formattedTime }}</p>
            <div class="wx">
              <i v-if="status" class="iconfont icon-weixin1"></i>
              <i v-else class="iconfont icon-zhifubao"></i>
            </div>
            <div class="switch" style="margin-top: 0" @click="switchPayment">
              切换至{{ status ? '支付宝' : '微信' }}
            </div>
            <div
              class="agrment"
              @click="agrment"
              style="font-size: 16px; color: #999999; margin-top: 14px"
            >
              支付即视为您同意<span style="color: #7eb1fc; cursor: pointer"
                >《……协议》</span
              >条例
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="detail">
      <div class="detail_title">课程详情</div>
      <div class="detail_box">
        <div class="content">
          <div v-if="classTopics.length">
            <el-row>
              <el-col :span="2.5"><span class="room">课程主题：</span></el-col>
              <el-col :span="21.5"
                ><span
                  v-for="item in classTopics"
                  :key="item.id"
                  class="font"
                  >{{ item.name + '   ' }}</span
                ></el-col
              >
            </el-row>
          </div>
          <div>
            <el-row>
              <el-col :span="2.5"><span class="room">课程标题：</span></el-col>
              <el-col :span="21.5"
                ><span class="font">{{ detailList.name }}</span></el-col
              >
            </el-row>
          </div>
          <el-row>
            <el-col :span="2.5"><span class="room">课程内容：</span></el-col>
            <el-col :span="21"
              ><span class="content-wrapper">
                <div
                  ref="contentRef"
                  class="font"
                  :class="{
                    contentInfo: !isExpanded,
                    'contentInfo-full': isExpanded,
                  }"
                >
                  {{ detailList.describe }}
                </div>
                <span
                  v-if="isEllipsisActive"
                  class="toggle-btn"
                  @click="toggleText"
                >
                  {{ isExpanded ? '收起' : '查看更多' }}
                </span>
              </span></el-col
            >
          </el-row>
          <!-- <div style="display: flex">
            <span class="room" style="display: block">课程内容：</span>
            
          </div> -->
          <div v-if="bannerInfoList?.off == 1">
            <div class="bg">
              <i class="iconfont icon-gou"></i>
              {{
                index == 0 ? '单' : index == 1 ? '全' : ''
              }}套课程视频无限次播放
            </div>
            <div class="bg">
              <i class="iconfont icon-gou"></i>课程视频不断实时更新
            </div>
            <div class="bg">
              <i class="iconfont icon-gou"></i>24小时VIP服务通道
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="code"
      v-if="bannerInfoList?.off == 1"
      style="align-items: center"
    >
      <div class="qrCode_left">
        <div class="qrCode" v-if="codeUrl">
          <img :src="codeUrl" alt="" class="qrImage" />
          <p style="font-size: 12px">{{ formattedTime }}</p>
          <div class="wx">
            <i v-if="status" class="iconfont icon-weixin1"></i>
            <i v-else class="iconfont icon-zhifubao"></i>
          </div>
        </div>
        <div class="switch" @click="switchPayment">
          切换至{{ status ? '支付宝' : '微信' }}
        </div>
      </div>
      <div class="pay_money">
        <div style="text-align: center">
          <span style="font-size: 18px; color: #cccccc">应付金额</span>
          <span style="display: inline-block; margin-left: 5px; color: #f84658"
            >¥<span style="font-size: 36px; color: #f84658">{{
              index == 0
                ? (detailList?.price ?? '0.00')
                : (resultPrice?.totalPrice?.discountedPrice ?? '0.00')
            }}</span
            >元</span
          >
        </div>
        <div class="agrment" @click="agrment">
          支付即视为您同意<span style="color: #7eb1fc; cursor: pointer"
            >《……协议》</span
          >条例
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ElMessage } from 'element-plus';
const emit = defineEmits(['payAll', 'changePay']);
const prop = defineProps({
  detailList: {
    type: Object,
    default: () => {},
  },
  codeUrl: {
    type: String,
    default: '',
  },
  formattedTime: {
    type: String,
    default: '',
  },
  imgLoading: {
    type: Boolean,
    default: false,
  },
  resultPrice: {
    type: Object,
    default: () => {},
  },
  bannerInfoList: {
    type: Object,
    default: () => {},
  },
});
import { computed, ref, nextTick } from 'vue';
import { useRouter } from 'vue-router';
const params = ref({
  id: '',
  type: '',
});
const router = useRouter();
const index = ref(0);
const status = ref(true);
const tabs = (val) => {
  status.value = true;
  index.value = val;
  if (val == 1) {
    if (prop.resultPrice?.totalPrice?.classTypeId) {
      params.value.id = prop.resultPrice?.totalPrice?.classTypeId?.split(',');
      params.value.type = 3;
      emit('payAll', params.value);
    } else {
      ElMessage.error('请在后台添加分类');
      return;
    }
  } else if (val == 2) {
    router.push({ path: '/zYshopDet' });
  }
};
const length = () => {
  const len = prop.resultPrice?.totalPrice?.classTypeId.split(',').length ?? 0;
  if (prop.resultPrice.totalPrice) {
    return (prop.resultPrice.totalPrice.price / len).toFixed(2);
  }
};
const classTopics = computed(() => {
  if (
    prop.resultPrice.data.length &&
    prop.detailList.type.split(',').filter((item) => item.trim() !== '')
      .length > 0
  ) {
    const type = prop.detailList.type.split(',');
    console.log(prop.resultPrice.data);
    return prop.resultPrice.data.filter((item) =>
      type.includes(String(item.id)),
    );
  }
  return [];
});

const isExpanded = ref(false);
const isEllipsisActive = ref(false);
const contentRef = ref(null);

// 切换文本显示
const toggleText = (val) => {
  if (val == 'false') {
    isExpanded.value = false;
  } else {
    isExpanded.value = !isExpanded.value;
  }
};

// 检查文本是否溢出
const checkOverflow = () => {
  nextTick(() => {
    if (contentRef.value) {
      isEllipsisActive.value =
        contentRef.value.scrollHeight > contentRef.value.clientHeight;
    }
  });
};
// 切换支付方式
const switchPayment = (val) => {
  if (val == 'true') {
    status.value = true;
  } else {
    status.value = !status.value;
  }

  emit('changePay', {
    params: params.value,
    index: index.value,
  });
};
// useragree
const agrment = () => {
  router.push({ path: '/useragree' });
};
// onMounted(() => {
//   checkOverflow();
// });
defineExpose({
  toggleText,
  checkOverflow,
  switchPayment,
});
</script>
<style lang="scss" scoped>
::v-deep(.el-popper) {
  width: 800px !important;
}
.tab_pay {
  display: flex;
}
.tab_pay_item {
  display: flex;
  width: 768px;
  align-items: center;
  line-height: 54px;
  background: linear-gradient(90deg, #fef5f6 0%, #fccfe1 100%);
  border-radius: 12px;
  margin-bottom: 24px;
  .containt {
    margin: 12px;
    width: 88px;
    height: 30px;
    line-height: 30px;
    background: linear-gradient(90deg, #ff7197 0%, #ff4d89 100%);
    border-radius: 32px;
    text-align: center;
    color: #fff;
    margin-right: 12px;
  }
  .txt {
    font-weight: 400;
    font-size: 16px;
    color: #72030d;
    line-height: 22px;
    text-align: left;
    font-style: normal;
    white-space: nowrap; /* 不换行 */
    overflow: hidden; /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 超出部分显示省略号 */
  }
}
.title {
  font-weight: 500;
  font-size: 21px;
  color: #333333;
  line-height: 29px;
  text-align: left;
  font-style: normal;
  margin-bottom: 12px;
  font-weight: bold;
  .infos {
    font-weight: 400;
    font-size: 14px;
    color: #999999;
    line-height: 20px;
    text-align: left;
    font-style: normal;
    margin-left: 5px;
    display: inline-block;
  }
}
.tab {
  .tab_left {
    position: relative;
    width: 222px;
    height: 166px;
    border-radius: 8px;
    border: 1px solid #e9ebed;
    text-align: center;
    font-style: normal;
    margin-right: 32px;
    cursor: pointer;
    .info {
      font-weight: 500;
      font-size: 18px;
      color: #333333;
      line-height: 25px;
      margin-top: 34px;
    }
    // .info.center {
    //   color: #f84658;
    // }
    .price {
      font-weight: 600;
      font-size: 36px;
      color: #333333;
      line-height: 50px;
      margin-left: -10px;
      .iconfont {
        font-weight: 600;
        font-size: 12px;
        color: #999999;
        text-align: left;
        font-style: normal;
      }
      // .iconfont.center {
      //   color: #f84658;
      // }
    }
    .price.center {
      font-weight: 600;
      font-size: 36px;
      // color: #f84658;
    }
    .strike-through {
      text-decoration: line-through; /* 添加横线 */
      font-weight: 400;
      font-size: 16px;
      color: #cccccc;
      line-height: 22px;
      text-align: center;
      font-style: normal;
    }
    .setInfo {
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      font-weight: 500;
      font-size: 14px;
      color: #666666;
      line-height: 25px;
      text-align: center;
      font-style: normal;
      background: #e9ebed;
      border-radius: 0px 0px 7px 7px;
    }
    .setInfo.center {
      background: linear-gradient(90deg, #fccce7 0%, #fddfe9 100%), #e9ebed;
      border-radius: 0px 0px 7px 7px;
      opacity: 0.6;
      font-weight: 500;
      font-size: 14px;
      color: #f84658;
    }
    .discounts {
      position: absolute;
      top: 0;
      right: 0;
      height: 25px;
      padding: 0 5px;
      background: linear-gradient(90deg, #f17c42 0%, #ea4029 100%);
      border-radius: 0px 7px 0px 7px;
      font-weight: 500;
      font-size: 12px;
      color: #ffffff;
      line-height: 25px;
    }
  }
  .tab_center {
    width: 462px;
    height: 166px;
    background: linear-gradient(180deg, #fef6f7 0%, #fef3f4 100%);
    border-radius: 8px;
    margin-right: 20px;

    .main {
      margin: 34px 50px 0 50px;
    }
  }
}
.middle {
  display: flex;
  justify-content: space-between;
  margin: 0 60px;
  align-items: center;
}
.fuhao {
  font-weight: 600;
  font-size: 36px;
  color: #333333;
  line-height: 50px;
  margin-top: 50px;
}
.more {
  display: flex;
  align-items: center;
  width: 25px;
  text-align: center;
  background: linear-gradient(180deg, #fef6f7 0%, #fff9fa 100%), #d8d8d8;
  border-radius: 8px;
  font-weight: 500;
  font-size: 12px;
  color: #333333;
  cursor: pointer;
}
.detail_title {
  font-weight: 500;
  font-size: 21px;
  color: #333333;
  margin-bottom: 12px;
  font-weight: bold;
}
.detail_box {
  width: 768px;
  background: linear-gradient(180deg, #fef6f7 0%, #fff9fa 100%);
  border-radius: 8px;
  border: 1px solid #f9707c;
  .content {
    margin: 10px;
    font-size: 16px;
  }
  .room {
    color: #999;
    display: inline-block;
    // width: 100%;
    // white-space: nowrap; /* 不换行 */
    // overflow: hidden; /* 超出部分隐藏 */
    // text-overflow: ellipsis; /* 超出部分显示省略号 */
  }
  .contentInfo {
    display: -webkit-box; /* 关键：启用多行省略 */
    -webkit-box-orient: vertical; /* 垂直排列文本 */
    -webkit-line-clamp: 1; /* 限制显示 3 行 */
    word-break: break-word; /* 防止长单词溢出 */
    overflow: hidden; /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 超出部分显示省略号 */
  }
  .bg {
    color: #f84658;
    font-weight: 400;
    font-size: 16px;
  }
  .icon-gou {
    font-size: 16px;
    margin-right: 5px;
  }
}
.detail {
  margin-top: 12px;
}
.code {
  display: flex;
  justify-content: center;
  .qrCode {
    height: 144px;
    position: relative;
    .qrImage {
      width: 144px;
      height: 144px;
    }
  }
  .wx {
    position: absolute;
    width: 30px;
    height: 30px;
    line-height: 30px;
    background-color: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 6px;
  }
  .icon-weixin1 {
    color: green;
    font-size: 24px;
  }
  .icon-zhifubao {
    color: rgb(6, 180, 253);
    font-size: 24px;
  }
}
.qrCode_left {
  text-align: center;
}
.switch {
  margin-top: 25px;
  cursor: pointer;
  font-weight: 400;
  font-size: 14px;
  color: #72030d;
}
.pay_money {
  margin-left: 24px;
}
.agrment {
  font-weight: 400;
  font-size: 16px;
  color: #999999;
}
.active {
  border: 1px solid #f9707c !important;
}
.cope {
  display: flex;
  justify-content: space-evenly;
}
.money {
  text-align: center;
  margin-top: 12px;
  // color: #f84658;
}
.money_info {
  font-weight: 500;
  font-size: 18px;
  color: #cccccc;
}
.money_price {
  font-weight: 600;
  font-size: 36px;
  margin-left: 5px;
  color: #f84658;
}
.toggle-btn {
  cursor: pointer;
  color: #409eff;
}
.font {
  color: #333333;
}
</style>
